home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 1999 June / maximum-cd-1999-06.iso / Fireworks 2 / data1.cab / Program_Files / Settings / Batch_Code / BatchTemplate.jst < prev   
Encoding:
Text File  |  1999-03-01  |  10.3 KB  |  311 lines

  1.  
  2. // Macromedia Fireworks Batch Script Template
  3. // Copyright (c) 1998, 1999 Macromedia. All rights reserved.
  4.  
  5. var batchArray = [
  6. #BATCH_SETTINGS#
  7. ];
  8.  
  9.     // ----------------------------------------------------------
  10.  
  11.     if (theDocList == null || theDocList.length == 0) {
  12.         theDocList = App.chooseScriptTargetDialog(App.getPref("MultiFileBatchTypes"));
  13.     }
  14.     
  15.     if (theDocList == null) {
  16.         // The user must have canceled the "select files" dialog.
  17.     } else if (theDocList.length == 0) {
  18.         // The user did something like "current files" when no files are open.
  19.         alert(Errors.ENoFilesSelected);
  20.     } else {
  21.         App.progressCountCurrent = 0;
  22.         App.progressCountTotal = theDocList.length;
  23.         for (var i = 0; i < theDocList.length; i++) {
  24.             App.progressCountCurrent = i + 1;
  25.             App.batchStatusString = "";
  26.             if (ProcessOneDocPath(theDocList[i]) == false)
  27.                 break;
  28.         }    
  29.     }
  30.  
  31.     // ----------------------------------------------------------
  32.  
  33.     function ProcessOneDocPath(docPathName)
  34.     {
  35.         var result = true;
  36.         var theDocWasOpen = false;
  37.         var theDoc = App.findOpenDocument(docPathName);
  38.  
  39.         if (theDoc == null) {
  40.             theDoc = App.openDocument(docPathName, false);
  41.             theDocWasOpen = false;
  42.         } else {
  43.             theDocWasOpen = true;
  44.         }
  45.  
  46.         if (ProcessOneDoc(theDoc) == false)
  47.             result = false;
  48.  
  49.         if (theDoc != null && theDocWasOpen == false)
  50.             theDoc.close(false);    // discard changes
  51.  
  52.         return result;
  53.     }
  54.  
  55.     // ----------------------------------------------------------
  56.  
  57.     function ProcessOneDoc(theDoc)
  58.     {
  59.  
  60.         // If sourceDocumentPath is null, the file is either a new document,
  61.         // was opened as untitled (e.g., via stationery), or is a nonnative
  62.         // file format (e.g., .psd). In these cases, get the revert file path.
  63.         // If *that* is null, abort.
  64.         var sourceDocumentPath = theDoc.filePathForSave;
  65.         if (sourceDocumentPath == null)
  66.             sourceDocumentPath = theDoc.filePathForRevert;
  67.         if (sourceDocumentPath == null) {
  68.             alert(Errors.EInternalError);
  69.             return false;
  70.         }
  71.         
  72.         App.batchStatusString = Files.getFilename(sourceDocumentPath);
  73.  
  74.         for (var i = 0; i < batchArray.length; i++) {
  75.             if (ProcessOneDocForOneBatch(theDoc, sourceDocumentPath, batchArray[i]) == false)
  76.                 return false;
  77.         }
  78.  
  79.         return true;
  80.     }
  81.     
  82.     // ----------------------------------------------------------
  83.  
  84.     function ProcessOneDocForOneBatch(theDoc, sourceDocumentPath, batch)
  85.     {
  86.         var backupPath = null;
  87.  
  88.         if (batch.doBackupFiles) {
  89.  
  90.             backupPath = GetBackupFile(sourceDocumentPath, batch.doIncrementalBackup);
  91.             if (backupPath == null) {
  92.                 alert(Errors.EFileNotFound);
  93.                 return false;
  94.             }
  95.  
  96.             var errorString = SafeMoveFileTo(sourceDocumentPath, backupPath);
  97.             if (errorString != null) {
  98.                 alert(errorString);
  99.                 return false;
  100.             }
  101.  
  102.             // Note that Files.swap(), and thus SafeMoveFileTo(), will change theDoc to
  103.             // refer to backupPath rather than sourceDocumentPath. We don't want this, 
  104.             // so we force the issue by setting it back to what we want. Note also that
  105.             // it is not necessarily the case that theDoc.filePathForSave == sourceDocumentPath
  106.             // (e.g., if the document was not originally a native Fireworks file), so we
  107.             // really only want to reset it if it changed.
  108.             if (theDoc.filePathForSave == backupPath) {
  109.                 theDoc.filePathForSave = sourceDocumentPath;
  110.             }
  111.         }
  112.  
  113.         if (batch.findAndReplaceParms != null) {
  114.             var theFinder = theDoc.makeFind(batch.findAndReplaceParms);        
  115.             var replacedAnything = theFinder.replaceAll();
  116.             if (theDoc.filePathForSave == null) {
  117.                 theDoc.filePathForSave = Document.makeGoodNativeFilePath(sourceDocumentPath);
  118.                 if (theDoc.filePathForSave == null) {
  119.                     // This should never happen, but check, just in case.
  120.                     alert(Errors.EInternalError);
  121.                     return false;
  122.                 }
  123.             }
  124.             if (replacedAnything) {
  125.                 // Save the changes.
  126.                 theDoc.save();
  127.             } else {
  128.                 // Don't save the file ... we didn't do anything.
  129.                 // But, if we are doing backups, make a copy of the original file
  130.                 // (now located in the Original Files folder) back in the original spot.
  131.                 if (batch.doBackupFiles && backupPath != null && sourceDocumentPath != null) {
  132.                     var errorString = SafeCopyFileTo(backupPath, sourceDocumentPath);
  133.                     if (errorString != null) {
  134.                         alert(errorString);
  135.                         return false;
  136.                     }
  137.                 }
  138.             }
  139.             if (theDoc.filePathForSave == null) {
  140.                 // This should never happen, but check, just in case.
  141.                 alert(Errors.EInternalError);
  142.                 return false;
  143.             }
  144.         }
  145.         
  146.         if (batch.exportOptions != null) {
  147.  
  148.             var curExportFormatOptions;
  149.             if (batch.exportOptions.useFormatOptionsFromEachFile) {
  150.                 curExportFormatOptions = theDoc.exportFormatOptions;
  151.             } else {
  152.                 curExportFormatOptions = Document.findExportFormatOptionsByName(batch.exportOptions.exportFormatOptions.name);
  153.                 if (curExportFormatOptions == null)
  154.                     curExportFormatOptions = batch.exportOptions.exportFormatOptions;
  155.             }
  156.             
  157.             // Copy the scaling/cropping info back over, since we may have
  158.             // gotten that info from the doc or the named settings,
  159.             // and want to override it here.
  160.             if (batch.exportOptions.exportFormatOptions != null) {
  161.                 curExportFormatOptions.applyScale = batch.exportOptions.exportFormatOptions.applyScale;
  162.                 curExportFormatOptions.useScale = batch.exportOptions.exportFormatOptions.useScale;
  163.                 curExportFormatOptions.percentScale = batch.exportOptions.exportFormatOptions.percentScale;
  164.                 curExportFormatOptions.xSize = batch.exportOptions.exportFormatOptions.xSize;
  165.                 curExportFormatOptions.ySize = batch.exportOptions.exportFormatOptions.ySize;
  166.                 curExportFormatOptions.cropTop = batch.exportOptions.exportFormatOptions.cropTop;
  167.                 curExportFormatOptions.cropLeft = batch.exportOptions.exportFormatOptions.cropLeft;
  168.                 curExportFormatOptions.cropBottom = batch.exportOptions.exportFormatOptions.cropBottom;
  169.                 curExportFormatOptions.cropRight = batch.exportOptions.exportFormatOptions.cropRight;
  170.                 curExportFormatOptions.crop = batch.exportOptions.exportFormatOptions.crop;
  171.             }
  172.                                 
  173.             // Always disable cropping.
  174.             curExportFormatOptions.crop = false;
  175.             
  176.             // If you want to actually modify the document, jam the settings back in here, like so:
  177.             //             theDoc.exportFormatOptions = curExportFormatOptions;
  178.             // We don't usually want to do this; instead, we pass the export settings
  179.             // as the (optional) second argument to exportTo(), which will leave the document
  180.             // unaffected.
  181.  
  182.             var theDir = Files.getDirectory(sourceDocumentPath);
  183.             var theName = Files.getFilename(sourceDocumentPath, true).toString();    // strip extension, if any, and ensure string-ness
  184.             if (batch.exportOptions.filenamePrefix != null)
  185.                 theName = batch.exportOptions.filenamePrefix + theName;
  186.             if (batch.exportOptions.filenameSuffix != null)
  187.                 theName = theName + batch.exportOptions.filenameSuffix;
  188.             
  189.             if (App.platform == "mac") {
  190.                 // Macintosh filenames are limited to 31 characters (including the extension)
  191.                 // which is easy to overflow by accident, and produces weird errors if we
  192.                 // try to use 'em. The exporter will typically append an extension to the
  193.                 // end, which may be up to 5 characters long (well, it actually could be longer
  194.                 // but rarely is). So we will constrain the base filename here to 26 characters
  195.                 // ( == 31 - 5) so that this problem is minimized.
  196.                 if (theName.length > 26) {
  197.                     theName = theName.substr(0, 26);
  198.                 }
  199.             }
  200.             
  201.             var exportPath = Files.makePathFromDirAndFile(theDir, theName);
  202.  
  203.             theDoc.exportTo(exportPath, curExportFormatOptions);
  204.         }
  205.         
  206.         return true;
  207.     }
  208.  
  209.     // ----------------------------------------------------------
  210.  
  211.     function UniquePathnameWithSameExtension(pathname)
  212.     {
  213.         if (Files.exists(pathname) == false) {
  214.             return pathname;    // already unique
  215.         }
  216.         
  217.         var filename = Files.getFilename(pathname).toString(); // make sure it's a string, not a number
  218.         var extension = "";
  219.         var curlength = filename.length;
  220.         for (var i = 1; i < curlength - 1; i++) {
  221.             if (filename.charAt(curlength - i) == ".") {
  222.                 extension = filename.substr(curlength - i);
  223.                 filename = filename.substr(0, curlength - i);
  224.                 break;
  225.             }
  226.         }
  227.  
  228.         var newpathname = pathname;
  229.         var newfilename = "";
  230.         for (var j = 1; j < 10000; j++) {
  231.             newfilename = filename + "-" + j + extension;
  232.             newpathname = Files.setFilename(pathname, newfilename);
  233.             if (Files.exists(newpathname) == false)
  234.                 return newpathname;
  235.         }
  236.         
  237.         // We should never get here.
  238.         return null;
  239.     }
  240.  
  241.     // ----------------------------------------------------------
  242.  
  243.     function GetBackupDirectory(pathname)
  244.     {
  245.         if (Files.exists(pathname) == false) {
  246.             return null;
  247.         }
  248.         
  249.         var dir = Files.getDirectory(pathname);
  250.         var dirName = App.getPref("OriginalFilesFolderName");
  251.         var bkupDir = Files.makePathFromDirAndFile(dir, dirName);
  252.  
  253.         if (Files.exists(bkupDir) == false)
  254.             Files.createDirectory(bkupDir);
  255.  
  256.         if (Files.exists(bkupDir) == false) {
  257.             return null;
  258.         }
  259.  
  260.         if (Files.isDirectory(bkupDir) == false) {
  261.             return null;
  262.         }
  263.             
  264.         return bkupDir;
  265.     }
  266.     
  267.     // ----------------------------------------------------------
  268.  
  269.     function GetBackupFile(pathname, doIncrementalBackup)
  270.     {
  271.         var bkupDir = GetBackupDirectory(pathname);
  272.         if (bkupDir == null)
  273.             return null;
  274.         var filename = Files.getFilename(pathname);
  275.         var backupFile = Files.makePathFromDirAndFile(bkupDir, filename);
  276.         if (doIncrementalBackup)
  277.             backupFile = UniquePathnameWithSameExtension(backupFile);
  278.         return backupFile;
  279.     }
  280.  
  281.     // ----------------------------------------------------------
  282.  
  283.     function SafeCopyFileTo(sourcePath, destPath)
  284.     {
  285.         if (Files.deleteFileIfExisting(destPath) == false)
  286.             return Files.getLastErrorString();
  287.  
  288.         if (Files.copy(sourcePath, destPath) == false)
  289.             return Files.getLastErrorString();
  290.         
  291.         return null;
  292.     }
  293.  
  294.     // ----------------------------------------------------------
  295.  
  296.     function SafeMoveFileTo(sourcePath, destPath)
  297.     {
  298.         if (Files.deleteFileIfExisting(destPath) == false)
  299.             return Files.getLastErrorString();
  300.  
  301.         // Note: if destPath exists, the two files are swapped; if destPath
  302.         // does not exist, sourcePath is moved to destPath. Either way,
  303.         // both source and dest must point to the same volume, or the call
  304.         // will fail.
  305.         if (Files.swap(sourcePath, destPath) == false)
  306.             return Files.getLastErrorString();
  307.         
  308.         return null;
  309.     }
  310.  
  311.